﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace Volume_Resource_Management
{
    public partial class frmDailyDeliveriesPerHFB : Form
    {
        string date;
        string HFB;
        string SM;
        volumesPerDay volume;
        string storeNumber;
        TimeSpan addition = new TimeSpan(0, 15, 0);

        List<departmentAssignment> rotaHours;
        List<employeeInformation> allHours;

        public frmDailyDeliveriesPerHFB(string dte, string hfb, string sm, string storeNo, volumesPerDay tdy)
        {
            InitializeComponent();
            date = dte;
            HFB = hfb;
            SM = sm;
            volume = tdy;
            storeNumber = storeNo;
        }

        public frmDailyDeliveriesPerHFB(string dte, string sm, string storeNo, volumesPerDay tdy)
        {
            InitializeComponent();
            date = dte;
            HFB = "0";
            SM = sm;
            volume = tdy;
            storeNumber = storeNo;
        }

        public void getVolumeInformation()
        {
            double vol = 0;

            string mixed = "";
            string full = "";
            string total = "";

            if (HFB != "0")
            {
                switch (HFB)
                {
                    case "01":
                        label1.Text += volume.HFB1[5].ToString() + " m³";
                        vol = volume.HFB1[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB1[0].ToString() + " @ " + Math.Round(volume.HFB1[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB1[2].ToString() + " @ " + volume.HFB1[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB1[4].ToString();

                        break;

                    case "02":
                        label1.Text += volume.HFB2[5].ToString() + " m³";
                        vol = volume.HFB2[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB2[0].ToString() + " @ " + Math.Round(volume.HFB2[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB2[2].ToString() + " @ " + volume.HFB2[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB2[4].ToString();

                        break;

                    case "03":
                        label1.Text += volume.HFB3[5].ToString() + " m³";
                        vol = volume.HFB3[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB3[0].ToString() + " @ " + Math.Round(volume.HFB3[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB3[2].ToString() + " @ " + volume.HFB3[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB3[4].ToString();

                        break;

                    case "04":
                        label1.Text += volume.HFB4[5].ToString() + " m³";
                        vol = volume.HFB4[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB4[0].ToString() + " @ " + Math.Round(volume.HFB4[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB4[2].ToString() + " @ " + volume.HFB4[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB4[4].ToString();

                        break;

                    case "06":
                        label1.Text += volume.HFB6[5].ToString() + " m³";
                        vol = volume.HFB6[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB6[0].ToString() + " @ " + Math.Round(volume.HFB6[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB6[2].ToString() + " @ " + volume.HFB6[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB6[4].ToString();

                        break;

                    case "07":
                        label1.Text += volume.HFB7[5].ToString() + " m³";
                        vol = volume.HFB7[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB7[0].ToString() + " @ " + Math.Round(volume.HFB7[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB7[2].ToString() + " @ " + volume.HFB7[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB7[4].ToString();

                        break;

                    case "08":
                        label1.Text += volume.HFB8[5].ToString() + " m³";
                        vol = volume.HFB8[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB8[0].ToString() + " @ " + Math.Round(volume.HFB8[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB8[2].ToString() + " @ " + volume.HFB8[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB8[4].ToString();

                        break;

                    case "09":
                        label1.Text += volume.HFB9[5].ToString() + " m³";
                        vol = volume.HFB9[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB9[0].ToString() + " @ " + Math.Round(volume.HFB9[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB9[2].ToString() + " @ " + volume.HFB9[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB9[4].ToString();

                        break;

                    case "10":
                        label1.Text += volume.HFB10[5].ToString() + " m³";
                        vol = volume.HFB10[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB10[0].ToString() + " @ " + Math.Round(volume.HFB10[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB10[2].ToString() + " @ " + volume.HFB10[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB10[4].ToString();

                        break;

                    case "11":
                        label1.Text += volume.HFB11[5].ToString() + " m³";
                        vol = volume.HFB11[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB11[0].ToString() + " @ " + Math.Round(volume.HFB11[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB11[2].ToString() + " @ " + volume.HFB11[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB11[4].ToString();

                        break;

                    case "12":
                        label1.Text += volume.HFB12[5].ToString() + " m³";
                        vol = volume.HFB12[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB12[0].ToString() + " @ " + Math.Round(volume.HFB12[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB12[2].ToString() + " @ " + volume.HFB12[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB12[4].ToString();

                        break;

                    case "13":
                        label1.Text += volume.HFB13[5].ToString() + " m³";
                        vol = volume.HFB13[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB13[0].ToString() + " @ " + Math.Round(volume.HFB13[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB13[2].ToString() + " @ " + volume.HFB13[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB13[4].ToString();

                        break;

                    case "14":
                        label1.Text += volume.HFB14[5].ToString() + " m³";
                        vol = volume.HFB14[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB14[0].ToString() + " @ " + Math.Round(volume.HFB14[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB14[2].ToString() + " @ " + volume.HFB14[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB14[4].ToString();

                        break;

                    case "15":
                        label1.Text += volume.HFB15[5].ToString() + " m³";
                        vol = volume.HFB15[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB15[0].ToString() + " @ " + Math.Round(volume.HFB15[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB15[2].ToString() + " @ " + volume.HFB15[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB15[4].ToString();

                        break;

                    case "16":
                        label1.Text += volume.HFB16[5].ToString() + " m³";
                        vol = volume.HFB16[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB16[0].ToString() + " @ " + Math.Round(volume.HFB16[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB16[2].ToString() + " @ " + volume.HFB16[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB16[4].ToString();

                        break;

                    case "17":
                        label1.Text += volume.HFB17[5].ToString() + " m³";
                        vol = volume.HFB17[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB17[0].ToString() + " @ " + Math.Round(volume.HFB17[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB17[2].ToString() + " @ " + volume.HFB17[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB17[4].ToString();

                        break;

                    case "18":
                        label1.Text += volume.HFB18[5].ToString() + " m³";
                        vol = volume.HFB18[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB18[0].ToString() + " @ " + Math.Round(volume.HFB18[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB18[2].ToString() + " @ " + volume.HFB18[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB18[4].ToString();

                        break;

                    case "19":
                        label1.Text += volume.HFB19[5].ToString() + " m³";
                        vol = volume.HFB19[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB19[0].ToString() + " @ " + Math.Round(volume.HFB19[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB19[2].ToString() + " @ " + volume.HFB19[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB19[4].ToString();

                        break;

                    case "92":
                        label1.Text += volume.HFB92[5].ToString() + " m³";
                        vol = volume.HFB92[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.HFB92[0].ToString() + " @ " + Math.Round(volume.HFB92[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.HFB92[2].ToString() + " @ " + volume.HFB92[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.HFB92[4].ToString();

                        break;
                }
            }
            else
            {
                switch (SM)
                {
                    case "1":
                        label1.Text += volume.SS[5].ToString() + " m³";
                        vol = volume.SS[5];

                        double mpVol = Convert.ToDouble(volume.SS[1]);
                        mpVol = Math.Round(mpVol, 2);

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.SS[0].ToString() + " @ " + Math.Round(volume.SS[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.SS[2].ToString() + " @ " + volume.SS[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.SS[4].ToString();

                        break;

                    case "2":
                        label1.Text += volume.FS[5].ToString() + " m³";
                        vol = volume.FS[5];

                        mixed = "Total Number Of Mixed Pallets ≈ " + volume.FS[0].ToString() + " @ " + Math.Round(volume.FS[1], 2).ToString() + "m³";
                        full = "Total Number Of Full Pallets ≈ " + volume.FS[2].ToString() + " @ " + volume.FS[3].ToString() + "m³";
                        total = "Total Number Of Pallets ≈ " + volume.FS[4].ToString();

                        break;
                }
            }

            double seconds = vol / 0.009;
            //MessageBox.Show(this, seconds.ToString());

            TimeSpan t = TimeSpan.FromSeconds(seconds);

            string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s",
                            t.Hours,
                            t.Minutes,
                            t.Seconds);

            label2.Text += " ≈ " + answer;
            label3.Text = mixed;
            label4.Text = full;
            label5.Text = total;
        }

        private void frmDailyDeliveriesPerHFB_Load(object sender, EventArgs e)
        {
            if (HFB != "0")
                this.Text = "HFB " + HFB + " Deliveries For " + date;
            else
                if (SM == "1")
                    this.Text = "Self Serve Deliveries For " + date;
                else
                    this.Text = "Full Serve Deliveries For " + date;

            List<ArticleStatus> aS;

            if (HFB != "0")
            {
                aS = databaseAccessability.getArticles(storeNumber, date, HFB, SM);
            }
            else
            {
                aS = databaseAccessability.getArticles(storeNumber, date, SM);
            }

            dataGridView1.DataSource = aS;

            this.dataGridView1.Columns["ID"].Visible = false;
            this.dataGridView1.Columns["Loc"].Visible = false;

            List<departmentAssignment> assignment = databaseAccessability.getWorkingEmployee(SM, HFB, storeNumber, date);

            foreach (departmentAssignment dA in assignment)
            {
                listBox1.Items.Add(dA.employeeName);
                listBox2.Items.Add(dA.workTime);
            }

            getVolumeInformation();
            getWorkingEmployees();
        }

        private void getWorkingEmployees()
        {
            rotaHours = databaseAccessability.getWorkingEmployee(storeNumber, date);
            allHours = databaseAccessability.getEmployeesPerDay(storeNumber, date);

            for (int i = 0; i < rotaHours.Count; i++)
            {
                for (int j = 0; j < rotaHours.Count; j++)
                {
                    if ((rotaHours[i].employeeName == rotaHours[j].employeeName) && (rotaHours[i].workTime != rotaHours[j].workTime))
                    {
                        string[] split = rotaHours[i].workTime.Split('-');
                        string[] split2 = rotaHours[j].workTime.Split('-');

                        TimeSpan one;
                        TimeSpan.TryParse(split[0], out one);

                        TimeSpan two;
                        TimeSpan.TryParse(split2[0], out two);

                        if (two < one)
                        {
                            string hours = two.ToString() + " -" + split[1];
                            rotaHours[i].workTime = hours;
                            rotaHours.RemoveAt(j);
                            break;
                        }

                        TimeSpan.TryParse(split[1], out one);
                        TimeSpan.TryParse(split2[1], out two);

                        if (two > one)
                        {
                            string hours = split[0] + " - " + two.ToString();
                            rotaHours[i].workTime = hours;
                            rotaHours.RemoveAt(j);
                            break;
                        }
                    }
                }
            }

            foreach (employeeInformation eI in allHours)
            {
                bool add = true;

                if (rotaHours.Count != 0)
                {
                    foreach (departmentAssignment dA in rotaHours)
                    {
                        if (eI.empName == dA.employeeName)
                        {
                            string hoursString = eI.startTime + ":00 - " + eI.endTime + ":00";

                            if (hoursString == dA.workTime)
                            {
                                add = false;
                            }
                        }
                    }

                    if (add)
                        comboBox1.Items.Add(eI.empName);
                }
                else
                {
                    comboBox1.Items.Add(eI.empName);
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            comboBox3.Items.Clear();
            comboBox3.Enabled = false;

            int allHrsNo = 0;
            List<int> rotaHrsNo = new List<int>();

            for(int i=0; i<allHours.Count; i++)
            {
                if (allHours[i].empName == comboBox1.Text)
                {
                    allHrsNo = i;
                    break;
                }
            }

            for (int i = 0; i < rotaHours.Count; i++)
            {
                if (rotaHours[i].employeeName == comboBox1.Text)
                {
                    rotaHrsNo.Add(i);
                }
            }

            DateTime from;
            DateTime to;

            DateTime.TryParse(allHours[allHrsNo].startTime, out from);
            DateTime.TryParse(allHours[allHrsNo].endTime, out to);

            TimeSpan tFrom = from - from.Date;
            TimeSpan tTo = to - to.Date;

            while (tFrom <= tTo)
            {
                comboBox2.Items.Add(tFrom.ToString());
                tFrom = tFrom.Add(addition);
            }

            if (rotaHrsNo.Count != 0)
            {
                for (int i = 0; i < rotaHrsNo.Count; i++)
                {
                    string[] splitString = rotaHours[rotaHrsNo[i]].workTime.Split('-');

                    DateTime frm;
                    DateTime t;

                    DateTime.TryParse(splitString[0], out frm);
                    DateTime.TryParse(splitString[1], out t);

                    TimeSpan tFrm = frm - frm.Date;
                    TimeSpan tT = t - t.Date;

                    while(tFrm <= tT)
                    {
                        for (int j = 0; j < comboBox2.Items.Count; j++)
                        {
                            if (comboBox2.Items[j].ToString() == tFrm.ToString())
                            {
                                comboBox2.Items.RemoveAt(j);
                                break;
                            }
                        }
                        tFrm = tFrm.Add(addition);
                    }
                }
            }
            comboBox2.Enabled = true;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox3.Enabled = true;

            List<List<TimeSpan>> rotaHrsNo = new List<List<TimeSpan>>();
            DateTime startTime;
            DateTime endTime;
            TimeSpan tStartTime;
            TimeSpan tEndTime;

            for (int i = 0; i < rotaHours.Count; i++)
            {
                if (rotaHours[i].employeeName == comboBox1.Text)
                {
                    List<TimeSpan> hrs = new List<TimeSpan>();

                    string[] split = rotaHours[i].workTime.Split('-');

                    DateTime.TryParse(split[0], out startTime);
                    DateTime.TryParse(split[1], out endTime);

                    tStartTime = startTime - startTime.Date;
                    tEndTime = endTime - endTime.Date;

                    hrs.Add(tStartTime);
                    hrs.Add(tEndTime);

                    rotaHrsNo.Add(hrs);
                }
            }

            comboBox3.Items.Clear();
            foreach (string value in comboBox2.Items)
            {
                comboBox3.Items.Add(value);
            }

            //remove all before startTime
            DateTime.TryParse(comboBox2.Text, out startTime);
            tStartTime = startTime - startTime.Date;
            
            for (int i = 0; i < comboBox3.Items.Count; i++)
            {
                DateTime comparison;
                DateTime.TryParse(comboBox3.Items[i].ToString(), out comparison);
                TimeSpan comp = comparison - comparison.Date;

                if (comp <= tStartTime)
                {
                    comboBox3.Items.RemoveAt(i);
                    i--;
                }
            }

            int deleteFrom = 0;
            bool delete = false;

            for (int i = 1; i < comboBox3.Items.Count; i++)
            {
                DateTime checkStart;
                DateTime checkFinish;

                DateTime.TryParse(comboBox3.Items[i - 1].ToString(), out checkStart);
                DateTime.TryParse(comboBox3.Items[i].ToString(), out checkFinish);

                TimeSpan tCheckStart = checkStart - checkStart.Date;
                TimeSpan tCheckFinish = checkFinish - checkFinish.Date;

                if ((tCheckFinish - tCheckStart) != addition)
                {
                    deleteFrom = i;
                    delete = true;
                    break;
                }
            }

            if (delete)
            {
                for (int i = deleteFrom; i < comboBox3.Items.Count; i++)
                {
                    comboBox3.Items.RemoveAt(i);
                    i--;
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox2.SelectedIndex = listBox1.SelectedIndex;
            button2.Enabled = true;
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.SelectedIndex = listBox2.SelectedIndex;
            button2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            databaseAccessability.deleteDepartmentSchedule(storeNumber, listBox1.SelectedItem.ToString(), listBox2.SelectedItem.ToString(), date);

            int delete = listBox1.SelectedIndex;

            listBox1.Items.RemoveAt(delete);
            listBox2.Items.RemoveAt(delete);

            comboBox2.Enabled = false;
            comboBox3.Enabled = false;

            comboBox1.Items.Clear();
            getWorkingEmployees();

            comboBox1.Text = "Select A Co-Worker";
            button2.Enabled = false;
          

        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            listBox1.Items.Add(comboBox1.Text);
            string time = comboBox2.Text + " - " + comboBox3.Text;
            listBox2.Items.Add(time);

            databaseAccessability.addAssignment(comboBox1.Text, time, HFB, SM, storeNumber, date);

            comboBox2.Enabled = false;
            comboBox3.Enabled = false;

            comboBox2.Text = "Start Time";
            comboBox3.Text = "Finish Time";

            comboBox1.Items.Clear();
            getWorkingEmployees();

            comboBox1.Text = "Select A Co-Worker";   
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1.Enabled = true;
        }
    }
}
